package com.ivanceras.client.widgets;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.DivElement;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ivanceras.shared.bo.Ad_field;
import com.ivanceras.shared.bo.Ad_tab;
import com.ivanceras.shared.db.DAO;
import com.ivanceras.shared.db.SerializableDAO;

public class ATab extends Composite {

	private static ATabUiBinder uiBinder = GWT.create(ATabUiBinder.class);

	interface ATabUiBinder extends UiBinder<Widget, ATab> {
	}
	
	@UiField HTMLPanel fieldPanel;
	@UiField DivElement tabName;

	Ad_field[] fieldList = null;
	List<AField> afieldList = new ArrayList<AField>();
	
	public ATab(Ad_tab tab) {
		initWidget(uiBinder.createAndBindUi(this));
		tabName.setInnerText(tab.getName());
		fieldList = tab.getAd_fieldList();
			for(Ad_field f : fieldList){
				AField field = new AField(f);
				afieldList.add(field);
				fieldPanel.add(field);
			}
	}

	public void setDAOContent(DAO[] daoList, Integer focusIndex) {
		DAO dao = daoList[focusIndex];
		displayDaoContent(dao);
	}
	
	public void displayDaoContent(DAO dao){
		for(int i = 0; i < fieldList.length; i++){
			Ad_field field = fieldList[i];
			String columnName = field.getAd_column().getColumnname().toLowerCase();
			Object value = dao.get_Value(columnName);
			afieldList.get(i).setValue(value);
		}
	}

}
